﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace Workflow.Alerts.Sublayouts.Reports
{
    public partial class GetExcel : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Page.ClientScript.RegisterStartupScript(typeof(Page), "RefreshParent", "<script language='javascript'>RefreshParent()</script>");
            var url = String.Format("~/Invoices{0}_{1}.csv", (Session[WorkflowConstants.CurrentUser] as User).MunicipalityId, DateTime.Now.ToString("MMddhh"));
            var path = Server.MapPath(url);
            
            Response.ContentType = "text/csv";
            Response.AppendHeader("Content-Disposition", "attachment; filename=Invoice.csv");
            
            FileStream fs = File.OpenRead(path);
            int fileLength = (int)(int)fs.Length;
            MemoryStream ms = new MemoryStream(fileLength);
            fs.Read(ms.GetBuffer(), 0, fileLength);
            fs.Close();
            File.Delete(path);
            Response.BinaryWrite(ms.GetBuffer());
            ms.Close();
            //Response.WriteFile(path);
            Response.End();

        }
    }
}
